// import { Water as ThreeWater } from "three/examples/jsm/objects/Water2";
import { BackSide, BufferGeometry, FrontSide, Mesh } from "three";
import WaterUtilty from "./utils/WaterUtilty";
import CustomPlaneGeometry from "../../renderExtension/geometriy/CustomPlaneGeometry";

/**
 * 水面对象
 */
export default class Water extends Mesh{ //ThreeWater {
    /**
 * 构造函数
 * @param options 配置项
 * @param options.points 水面多边形轮廓（闭合）
 * @param options.color 水面颜色,ffffff为透明
 * @param options.scale 水纹大小，此参数越大，单个水纹面积越大
 * @param options.reflectivity 反射度[0-1]，值越大，水面反射的倒影颜色越重
 * @param options.flowX 水流X方向比例（用于控制水的流动方向）
 * @param options.flowY Y方向水流比例（用于控制水的流动方向）
 * @example  let pointArray = [ //3857表示web墨卡托坐标系，4326表示经纬度坐标
    new GE.Point(13376713.90129997, 3540579.2959903134, 0, '3857'),
    new GE.Point(13376711.98134067, 3539826.5375406537, 0, '3857'),
    new GE.Point(13377952.49603797, 3539759.3270052206, 0, '3857'),
    new GE.Point(13378008.185227688, 3540675.3111583497, 0, '3857'),
    new GE.Point(13377184.375437068, 3540339.2581293588, 0, '3857')
];
let water = new GE.Water({
    points: pointArray,
    color: '#ffffff', //水面颜色，ffffff为透明色
    scale: 4, //水纹大小
    reflectivity: 1, //反射度
    flowX: 1, //X方向水流比例（用于控制水的流动方向）[0-1]
    flowY: 1 //Y方向水流比例（用于控制水的流动方向）[0-1]
});
app.add(water);
 */
    public constructor(options: {
        points: Array<any>,
        color: string,
        scale: number,
        reflectivity: number,
        flowX: number,
        flowY: number
    }) {
        super(new BufferGeometry(), null);
        
        WaterUtilty.setWater(this, options);
        this.makeGeometry(options.points);
    }

    private makeGeometry(points) {
        this['geometry'] = new CustomPlaneGeometry(points, true);
        let earClipData = (this['geometry'] as CustomPlaneGeometry).earClipData;

        this['position'].set(earClipData.firstPoint.x, earClipData.firstPoint.y, 10);

        if(earClipData.isCCW){
            this.material['side'] = FrontSide;
        }else{
            this.material['side'] = BackSide;
        }
    }
}